package com.jic.product.controller.manage;

import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.CategoryPropertyName;
import com.jic.product.manage.request.CategoryPropertyNameListRequest;
import com.jic.product.manage.request.CategoryPropertyNameRequest;
import com.jic.product.manage.request.ProductParentRequest;
import com.jic.product.manage.response.CategoryPropertyNameResponse;
import com.jic.product.service.CategoryPropertyNameService;
import com.jic.product.service.ProductPropertyNameRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WS on 2020/2/7  20: 19
 * 类目属性管理
 */
@RestController
@RequestMapping("/productCategoryPropertyName")
@Api(value = "类目属性", tags = {"类目属性"})
@Slf4j
public class CategoryPropertyNameController {

    @Autowired
    CategoryPropertyNameService categoryPropertyNameService;

    @Autowired
    ProductPropertyNameRelationService productPropertyNameRelationService;

    @ResponseBody
    @PostMapping(value = "/getCategoryPropertyName")
    @ApiOperation(value = "查询类目属性和值")
    public RestResult<List<CategoryPropertyNameResponse>> getCategoryPropertyName(@RequestBody CategoryPropertyNameRequest categoryPropertyNameRequest) {
        return categoryPropertyNameService.getCategoryPropertyName(categoryPropertyNameRequest);
    }

    @ResponseBody
    @PostMapping(value = "/savePropertyNameRelation")
    @ApiOperation(value = "保存商品类目属性和值")
    public RestResult<Boolean> savePropertyNameRelation(@RequestBody ProductParentRequest productParentRequest) {
        return productPropertyNameRelationService.savePropertyNameRelation(productParentRequest);
    }
    @ResponseBody
    @PostMapping(value = "/saveCategoryPropertyName")
    @ApiOperation(value = "保存类目属性和值")
    public RestResult<Boolean> saveCategoryPropertyName(@RequestBody CategoryPropertyNameListRequest categoryPropertyNameListRequest) {
        return categoryPropertyNameService.saveCategoryPropertyName(categoryPropertyNameListRequest);
    }


    @ResponseBody
    @PostMapping(value = "/deleteCategoryProperty")
    @ApiOperation(value = "删除类目属性")
    public RestResult deleteCategoryProperty(@RequestBody CategoryPropertyNameRequest categoryPropertyNameRequest) {
        log.info("jic-product===============》删除类目属性,{}", categoryPropertyNameRequest.toString());
        try {
            categoryPropertyNameService.delete(categoryPropertyNameRequest.getId());
        } catch (Exception e) {
           log.error("deleteCategoryProperty==========>"+e);
        }
        return  RestResult.success(true);
    }
}
